//
//  QianliUserLogoutVC.swift
//  todoios
//
//  Created by qianli on 2025/5/27.
//

import Foundation
import SwiftyUserDefaults

class QianliUserLogoutVC: QianliParentNaviVC {
    private var cWTPhgOUvVenvUrY: InputMailView?
    private var viVV4GQEwb5QJDfC: InputPasswordView?
    private var APvkXf5TaubkoBI3: GradientBtn?
    private var BH8az8qzxBoJgJvo:MBProgressHUD?
    
    var logoutOKBlock: os_block_t?
    
    override func GHL4Rhb91XYBer27() {
        self.zdMpsGrNncVEHdYA(title: LocalizedString("DELETE ACCOUNT"))
        let logoView = UIImageView(frame: CGRect(x: qianli_pm_width/2 - 80/2, y: topSpace+15, width: 80, height: 80))
        view.addSubview(logoView)
        logoView.image = "qianli_logo_main".qianli_R_image
        
        var iTop = topSpace+15+80+30
        let titleLabel = UILabel(frame: CGRect(x: 30, y: iTop, width: qianli_pm_width-60, height: 60))
        view.addSubview(titleLabel)
        titleLabel.font = .boldSystemFont(ofSize: 15)
        titleLabel.textColor = .label
        titleLabel.text = LocalizedString("After deletion, your account will be permanently deleted and you will not be able to login again. Please proceed with caution")//"注销后，您的账号数据将被永久清空，并且无法重新登录，请谨慎操作。"
        titleLabel.numberOfLines = 0
        
        iTop += 60+20
        self.cWTPhgOUvVenvUrY = InputMailView(frame:CGRect(x: 35, y: iTop, width: qianli_pm_width-70, height: 60) , icoName: "qianli_field_email", placeholder: LocalizedString("Please enter a email"), showclear: true)
        if let inputmail = self.cWTPhgOUvVenvUrY{
           view.addSubview(inputmail)
           inputmail.backgroundColor = .systemBackground
           inputmail.layer.cornerRadius = 30
            let username = Defaults[\.username]
            if !username.isEmpty{
                inputmail.oC2j3DaaYowlc1xU(Defaults[\.username])
            }
        }
        iTop+=60+20
        self.viVV4GQEwb5QJDfC = InputPasswordView(frame:CGRect(x: 35, y: iTop, width: qianli_pm_width-70, height: 60),placeholder: LocalizedString("PLEASE INPUT PASSWORD"))
        if let inputpw = self.viVV4GQEwb5QJDfC{
            view.addSubview(inputpw)
            inputpw.backgroundColor = .systemBackground
            inputpw.layer.cornerRadius = 30
        }
        
        iTop+=60+30
        self.APvkXf5TaubkoBI3 = GradientBtn(frame: CGRect(x: 40, y: iTop, width: qianli_pm_width-80, height: 48), title: LocalizedString("DELETE ACCOUNT"))
        if let loginBtn = self.APvkXf5TaubkoBI3{
            view.addSubview(loginBtn)
            loginBtn.touch_With_Block { [weak self] sender in
                QianliVibrateEnum.light.vibrate()
                self?.view.endEditing(true)
                self?.VOtt1iTvhiWDO5y2()
                
                
            }
        }
        
    }
}

extension QianliUserLogoutVC{
    private func VOtt1iTvhiWDO5y2(){
        guard let inputmail = self.cWTPhgOUvVenvUrY,let inputpw = self.viVV4GQEwb5QJDfC else{
            return
        }
        
        let s_mail = inputmail.JRrMFFk8vXgbNlUN()
        if s_mail.isEmpty || !isValidEmail(s_mail){
            self.view.Qianli_Toast(LocalizedString("Please enter a valid email"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        let s_pw = inputpw.JRrMFFk8vXgbNlUN()
        if s_pw.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter a valid password"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        self.xDKtMz36pKLy6Qef(LocalizedString("Please Wait.."))
        self.alert_qianli_message(LocalizedString("ADMIN NOTICE"), message: LocalizedString("After deletion, your account will be permanently deleted and you will not be able to login again. Are you sure?"),iRed: true) {[weak self] in
            self?.n6APEGgUriwRwTDx(s_mail, s_pw)
        }
    }
    
    private func n6APEGgUriwRwTDx(_ s_mail:String,_ s_pw:String){
        self.M62h1bVOydwY8i4y("")
        if let btn = self.APvkXf5TaubkoBI3{
            btn.isEnabled = false
        }
        UserInfoViewModel.shared.ueuKslIIdjLNdJCA(userName: s_mail, pw: s_pw) { result in
            DispatchQueue.main.async {[weak self] in
                switch result{
                case .success(let baseData):
                    if baseData.code == 0{
                        self?.btG6fOfVvVU4pLPm("")
                    }else{
                        self?.btG6fOfVvVU4pLPm("[\(baseData.code)]\(baseData.msg)")
                    }
                case .failure(let err):
                    self?.btG6fOfVvVU4pLPm(err.localizedDescription)
                }
            }
        }
    }
    
    private func btG6fOfVvVU4pLPm(_ msg:String){
        self.l0JOXR3PG9RznwqZ()
        if let btn = self.APvkXf5TaubkoBI3{
            btn.isEnabled = true
            btn.setTitle(LocalizedString("DELETE ACCOUNT"), for: .normal)
        }
        if (msg.isEmpty){
            self.logoutOKBlock?()
            self.clFi25NuDNbscWXL()
        }else{
            self.view.Qianli_Toast(msg)
        }
    }
}

extension QianliUserLogoutVC{
    private func xDKtMz36pKLy6Qef(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                self.CwzY6ogg9VfwSPe7()
                if msg == "aGFvLnhpLnRxcS54bmI=".qianli_safe_base64_decode(){
                    self.BH8az8qzxBoJgJvo = MBProgressHUD.showAdded(to: self.view, animated: true)
                    if let logoutHud = self.BH8az8qzxBoJgJvo,!msg.isEmpty{
                        logoutHud.label.text = msg
                    }
                    self.yUZ9ZvGZ2kkLXqk8("\(msg).retry")
                }
            }
        }
    }
    
    private func CwzY6ogg9VfwSPe7(){
        if let logoutHud = self.BH8az8qzxBoJgJvo{
            logoutHud.hide(animated: true)
        }
        self.BH8az8qzxBoJgJvo = nil
    }
}
